package multiple_forms;

//多态,重写,向上转型
//父类
class Animal {
    public String name;
    public Animal(String name) {
        this.name=name;
    }

    //重写
    public void eat() {
        System.out.println(name + "吃饭");
    }
}
//Dog子类
class Dog extends Animal {
    public Dog(String name) {
        super(name);  //调用父类的构造方法进行初始化
    }

    @Override
    public void eat() {
        System.out.println(super.name + "吃狗粮");
    }
}
//Cat子类
class Cat extends Animal {
    public Cat(String name) {
        super(name); //调用父类的构造方法
    }

    @Override
    public void eat() {
        System.out.println(super.name + "吃猫粮");
    }
}

//Mulf类
public class Mulf {
    //向上转型法一
    public static void func(Animal animal) {
        animal.eat();
    }
    public static void main(String[] args) {
        Dog dog = new Dog("旺财2.0");
        func(dog);
        Cat cat = new Cat("咪咪2.0");
        func(cat);
    }
    //向上转型法二
    public static void main(String[] args) {
        Animal animal = new Dog("旺财");  //向上转型

        Animal animal1 = new Cat("咪咪"); //向上转型
        animal.eat();
        animal1.eat();
    }
    //向上转型法三
    public static Animal func(String var) {
        if(var=="狗") {
            return new Dog("旺财3.0");
        }else if(var=="猫") {
            return new Cat("咪咪3.0");
        }else {
            return null;
        }
    }
    public static void main(String[] args) {
        func("狗").eat();
    }
}

//多态
class Shape {
    //属性...
    public void draw() {
        System.out.println("画图形");
    }
}
class Rate extends Shape {
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}
class Cycle extends Shape {
    @Override
    public void draw() {
        System.out.println("画圆");
    }
}
class Flower extends Shape {
    @Override
    public void draw() {
        System.out.println("画花");
    }
}

public class Mulf {
    public static void drawShapes() {
        Shape[] shapes = {new Cycle(),new Rate(),new Flower()};
        for (Shape shape:shapes) {
            shape.draw();
        }
    }
    public static void main(String[] args) {
        drawShapes();
    }
}
